package com.igroupes.ovineherd.model.attribute.date;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.igroupes.ovineherd.dto.response.Meta;
import com.igroupes.ovineherd.entity.DataModelAttributeEntity;
import com.igroupes.ovineherd.enums.BooleanKey;
import com.igroupes.ovineherd.exception.ModelException;
import com.igroupes.ovineherd.model.IParentModelData;
import com.igroupes.ovineherd.model.Style.TimeRangeStyle;
import com.igroupes.ovineherd.model.dto.FieldApiResponse;
import com.igroupes.ovineherd.model.enums.FormKey;
import com.igroupes.ovineherd.model.enums.MySqlTypeKey;
import com.igroupes.ovineherd.model.utils.ListUtils;
import com.igroupes.ovineherd.query.FieldQuery;
import com.igroupes.ovineherd.query.ModelDataJsonAttributeQuery;
import com.igroupes.ovineherd.util.DateUtils;
import com.igroupes.ovineherd.util.StringUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.util.ArrayList;
import java.util.List;

/**
 * 时间范围
 */
@EqualsAndHashCode(callSuper = true)
@Data
@ToString(callSuper = true)

public class TimeRangeModelData extends TimeBasicAttribute{

    public TimeRangeModelData() {
        super();
    }

    public Boolean verifyFieldV2(TimeRangeModelData basic) {
        return super.verifyFieldV2(basic);
    }

    @Override
    public List<FieldQuery> sqlField(IParentModelData parentModelData, DataModelAttributeEntity dataModelAttributeEntityOld) {
        TimeRangeModelData dateBasicAttribute = (TimeRangeModelData) parentModelData.getAttributes();
        List<FieldQuery> fieldQueries = super.sqlField(parentModelData,dataModelAttributeEntityOld);
        FieldQuery fieldQuery = fieldQueries.get(0);
        DataModelAttributeEntity dataModelAttributeEntity = fieldQuery.getDataModelAttributeEntity();
        JSONObject jsonObject = dataModelAttributeEntity.getJson();
        jsonObject.set("maxDate", Convert.toStr(dateBasicAttribute.getMaxDate().getValue()));
        jsonObject.set("minDate", Convert.toStr(dateBasicAttribute.getMinDate().getValue()));
        dataModelAttributeEntity.setJson(jsonObject);
        dataModelAttributeEntity.setDbNumber(2);
        fieldQuery.setLength(8);
        fieldQuery.setDataModelAttributeEntity(dataModelAttributeEntity);
        fieldQueries.set(0,fieldQuery);

//        FieldQuery fieldQuery1 = new FieldQuery();
//        BeanUtil.copyProperties(fieldQuery, fieldQuery1);
//        fieldQuery1.setFieldName(fieldQuery.getFieldName()+"1");
//        dataModelAttributeEntity.setDbName(fieldQuery.getFieldName()+"1");
//        fieldQuery1.setDataModelAttributeEntity(null);
//        fieldQueries.add(fieldQuery1);
        return fieldQueries;
    }
    @Override
    public List<FieldApiResponse> verifyRequestData(DataModelAttributeEntity modelAttributeEntity, Object value) {
        super.verifyRequestData(modelAttributeEntity, value);
        List<FieldApiResponse> fieldApiResponses = new ArrayList<>();
        if(value == null){
            return fieldApiResponses;
        }
        JSONObject jsonObject = modelAttributeEntity.getJson();
        String minDate = jsonObject.get("minDate").toString();
        String maxDate = jsonObject.get("maxDate").toString();

        if(!(value instanceof ArrayList)){
            throw new ModelException("传值格式错误！");
        }
        ArrayList<String> dateStyleValue = (ArrayList) value;
        if(dateStyleValue.size() > 0) {
            if (StringUtils.isNotBlank(dateStyleValue.get(0))) {
                if (!DateUtils.checkDate(dateStyleValue.get(0), DateUtils.TIME_FORMAT)) {
                    throw new ModelException(modelAttributeEntity.getName() + "传值格式不正确");
                }
            }
            if (StringUtils.isNotBlank(dateStyleValue.get(1))) {
                if (!DateUtils.checkDate(dateStyleValue.get(1), DateUtils.TIME_FORMAT)) {
                    throw new ModelException(modelAttributeEntity.getName() + "传值格式不正确");
                }
            }
            if (StringUtils.isNotBlank(dateStyleValue.get(0))) {
                if(StringUtils.isNotBlank(minDate)){
                    if (!DateUtils.timeVerification(minDate, dateStyleValue.get(0), DateUtils.TIME_FORMAT)) {
                        throw new ModelException(modelAttributeEntity.getName() + "传值错误，时间不能小于" + minDate);
                    }
                }
                if(StringUtils.isNotBlank(maxDate)) {
                    if (!DateUtils.timeVerification(dateStyleValue.get(0), maxDate, DateUtils.TIME_FORMAT)) {
                        throw new ModelException(modelAttributeEntity.getName() + "传值错误，时间不能大于" + maxDate);
                    }
                }
            }
            if(StringUtils.isNotBlank(dateStyleValue.get(1))){
                if(StringUtils.isNotBlank(minDate)) {
                    if (!DateUtils.timeVerification(minDate, dateStyleValue.get(1), DateUtils.TIME_FORMAT)) {
                        throw new ModelException(modelAttributeEntity.getName() + "传值错误，时间不能小于" + minDate);
                    }
                }
                if(StringUtils.isNotBlank(maxDate)) {
                    if (!DateUtils.timeVerification(dateStyleValue.get(1), maxDate, DateUtils.TIME_FORMAT)) {
                        throw new ModelException(modelAttributeEntity.getName() + "传值错误，时间不能大于" + maxDate);
                    }
                }
            }
            if(StringUtils.isNotBlank(dateStyleValue.get(0)) && StringUtils.isNotBlank(dateStyleValue.get(1))) {
                if (!DateUtils.timeVerification(dateStyleValue.get(0), dateStyleValue.get(1), DateUtils.TIME_FORMAT)) {
                    throw new ModelException(jsonObject.get("leftName") + "不能大于" + jsonObject.get("rightName"));
                }
            }
        }

        FieldApiResponse fieldApiResponse = new FieldApiResponse();
        fieldApiResponse.setDbName(modelAttributeEntity.getDbName());
        fieldApiResponse.setValue(ListUtils.toListString(dateStyleValue));
        fieldApiResponse.setIsStr(true);
        fieldApiResponse.setType(MySqlTypeKey.VARCHAR.getCode());
        fieldApiResponses.add(fieldApiResponse);
        return fieldApiResponses;
    }

    @Override
    public JSONObject getAttributeStyle(ModelDataJsonAttributeQuery dataModelAttributeEntity) {
        JSONObject jsonObject = dataModelAttributeEntity.getJson();
        String minDate = jsonObject.get("minDate").toString();
        String maxDate = jsonObject.get("maxDate").toString();
        return new JSONObject(new TimeRangeStyle(
                dataModelAttributeEntity.getName(),new ArrayList<>(),
                dataModelAttributeEntity.getIsNull().equals(BooleanKey.TRUE.getCode())
                ,minDate, maxDate
        ), false);

    }
    @Override
    public Meta getMeta(ModelDataJsonAttributeQuery dataModelAttributeEntity){
        return new Meta(FormKey.TIME.getCode(), true, true, true);
    }


    @Override
    public Object getData(DataModelAttributeEntity modelAttributeEntity, Object value) {
        if(ObjectUtil.isNotNull(value)){
            return ListUtils.parseString(value.toString());
        }
        return value;
//        return super.getData(modelAttributeEntity, value);
    }
}
